<template>
  <div>
    <el-card
      class="box-card"
      shadow="hover"
    >
      <div
        slot="header"
        class="clearfix"
      >
        <span>添加专业</span>
      </div>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input
            placeholder="请输入要添加的专业"
            v-model="inputAddText"
            clearable
            style="width: 15rem"
          >
          </el-input>
          <el-button
            type="primary"
            @click="add"
            style="margin-left: 5px"
          >添加</el-button>
        </el-col>
        <el-col :span="12">
          <el-input
            placeholder="请输入查找专业"
            v-model="inputSearchText"
            clearable
            style="width: 15rem"
          >
          </el-input>
          <el-button
            type="primary"
            @click="tipSearch"
            style="margin-left: 5px"
          >查找</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-divider></el-divider>
    <el-card
      class="box-card"
      shadow="hover"
      v-if="showEdit == false"
    >
      <div
        slot="header"
        class="clearfix"
      >
        <span>所有专业</span>
      </div>
      <el-table
        :data="tableData"
        border
        :default-sort="{ prop: 'c_id', order: 'descending' }"
        style="width: 100%"
      >
        <el-table-column
          prop="d_id"
          sortable
          label="专业编号"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="departments"
          label="专业名称"
          align="center"
        > </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          min-width='150px'
        >
          <template slot-scope="scope">
            <el-button
              size="small"
              type="danger"
              @click="open(scope.$index, scope.row)"
            >
              删除
            </el-button>
            <el-button
              size="small"
              type="primary"
              @click="cshow(scope.$index, scope.row)"
            >
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage1"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 2rem; text-align: center"
      >
      </el-pagination>
    </el-card>
    <el-card
      style="margin-top: 20px"
      v-else-if="showEdit == true"
    >
      <el-page-header
        @back="goBack"
        content="详情页面"
      > </el-page-header>
      <br />
      <el-form
        ref="form"
        v-model="form"
        label-width="80px"
        style="width: 50%"
      >
        <el-form-item label="班级编号">
          <el-input
            v-model="form.did"
            disabled
            style="width: 40%"
          ></el-input>
        </el-form-item>
        <el-form-item label="班级名称">
          <el-input
            v-model="form.dname"
            style="width: 40%"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="updata"
          >提交修改</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {
  addDepartment,
  upDepartmentdata,
  getAllDepartments,
  getDepartmentSear,
  delDepartmentdata,
} from "../../api/admin-api/departmentsAdmin";
export default {
  name: "DepartmentAdmin",
  pageSize: 10,
  pageNo: 1,
  data() {
    return {
      total: 0,
      currentPage1: 0,
      tableData: [],
      currPage: 0,
      pageNum: 10,
      inputAddText: "",
      inputSearchText: "",
      showEdit: false,
      form: {
        did: "",
        dname: "",
      },
    };
  },
  created() {
    this.getDepartment();
  },
  methods: {
    // 获取所有专业
    getDepartment() {
      getAllDepartments().then((res) => {
        this.tableData = res.data.data;
        this.total = res.data.data.length;
      });
    },

    add() {
      if (!this.inputAddText) this.open3("提交添加专业不可为空");
      else {
        addDepartment(this.inputAddText)
          .then((res) => {
            this.open2("添加专业成功");
            this.getDepartment();
          })
          .catch((err) => {
            this.open3(err);
          });
      }
    },

    // 显示修改页面
    cshow(index, row) {
      this.showEdit = true;
      this.form.did = row.d_id;
      this.form.dname = row.departments;
    },

    // 编辑返回
    goBack() {
      this.showEdit = false;
    },

    // 更新
    updata() {
      if (!this.form.did || !this.form.dname) {
        this.open3("不可为空");
      } else {
        upDepartmentdata(this.form.did, this.form.dname)
          .then((res) => {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
            this.showEdit = false;
            this.getDepartment(this.pageSize, this.pageNo);
          })
          .catch((err) => {
            this.open3(err);
          });
      }
    },

    // 删除弹框
    open(index, row) {
      this.$confirm("此操作将删除专业, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.del(index, row);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 删除
    del(index, row) {
      delDepartmentdata(row.d_id).then((res) => {
        this.getDepartment(this.pageSize, this.pageNo);
      });
    },

    tipSearch() {
      getDepartmentSear(this.inputSearchText, this.currPage, this.pageNum).then(
        (res) => {
          this.tableData = res.data.data;
          this.total = res.data.data.length;
        }
      );
    },

    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      //自行添加分页
    },
    handleCurrentChange(val) {
      // console.log(`当前页${val} `);
      this.pageNo = val;
    },
    open1(v) {
      this.$message(v);
    },
    open2(v) {
      this.$message({
        message: v,
        type: "success",
      });
    },
    open3(v) {
      this.$message({
        message: v,
        type: "warning",
      });
    },
    open4(v) {
      this.$message.error(v);
    },
  },
};
</script>

<style scoped>
</style>